package com.kgcx.organization.rabbitmq.producer;

import com.alibaba.fastjson.JSONObject;
import com.kgcx.common.rabbitmq.bean.Message;
import com.kgcx.common.rabbitmq.consts.ExchangeConst;
import com.kgcx.common.rabbitmq.consts.RoutingKeyConst;
import com.kgcx.core.utils.JsonUtil;
import com.kgcx.dbutils.service.DictService;
import com.kgcx.organization.domain.Driver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author 吴刚林
 * @date 2017/11/20
 */
@Slf4j
@Component
public class DriverProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private DictService dictService;

    private static final String TYPE = "baseInfoDriver";

    @Async
    public void addDriver(Driver driver) {
        log.info(String.format("新增司机记录新增成功的MQ消息，司机id[%s]。", driver.getKid()));
        Message msg = Message.buildMessage(TYPE, Message.FLAG_ADD, dictService.buildDictValue(driver));
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON,
                JSONObject.toJSONString(msg));
    }

    @Async
    public void updateDriver(Driver driver) {
        log.info(String.format("修改司机记录新增成功的MQ消息，司机id[%s]。", driver.getKid()));
        Message msg = Message.buildMessage(TYPE, Message.FLAG_UPDATE, dictService.buildDictValue(driver));
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON,
                JsonUtil.toString(msg));
    }

    @Async
    public void deleteDriver(Driver driver) {
        log.info(String.format("修改司机记录新增成功的MQ消息，司机id[%s]。", driver.getKid()));
        Message msg = Message.buildMessage(TYPE, Message.FLAG_DELETE, dictService.buildDictValue(driver));
        rabbitTemplate.convertAndSend(ExchangeConst.OPC, RoutingKeyConst.OPC_COMMON,
                JsonUtil.toString(msg));
    }


}
